package com.ljf.common.utils;

import com.ljf.common.enums.BasicEnum;
import com.ljf.common.enums.BasicStringEnum;
import com.ljf.common.result.ResultModel;
import com.ljf.common.vo.BasicEnumVO;
import com.ljf.common.vo.BasicStringEnumVO;

import java.util.*;

/**
 * 枚举工具类
 *
 * @author LJF
 * @since 2023-08-30
 */
public class EnumUtil {

    private EnumUtil() {

    }

    /**
     * 获取枚举返回
     *
     * @param dataList
     * @param <T>
     * @return
     */
    public static <T extends BasicEnum> List<BasicEnumVO> getEnumVo(T[] dataList) {
        List<BasicEnumVO> resultList = new ArrayList<>();
        for (T t : dataList) {
            resultList.add(
                    new BasicEnumVO(t.getCode(), t.getName())
            );
        }
        return resultList;
    }

    /**
     * 获取枚举返回
     *
     * @param dataList
     * @param <T>
     * @return
     */
    public static <T extends BasicStringEnum> List<BasicStringEnumVO> getEnumVo(T[] dataList) {
        List<BasicStringEnumVO> resultList = new ArrayList<>();
        for (T t : dataList) {
            resultList.add(
                    new BasicStringEnumVO(t.getCode(), t.getName())
            );
        }
        return resultList;
    }

    /**
     * 获取枚举返回 封装
     *
     * @param dataList
     * @return
     * @param <T>
     */
    public static <T extends BasicEnum> ResultModel getEnumVoResult(T[] dataList) {
        List<BasicEnumVO> enumList = getEnumVo(dataList);
        return ResultUtil.success(enumList);
    }

    /**
     * 获取枚举返回 封装
     *
     * @param dataList
     * @return
     * @param <T>
     */
    public static <T extends BasicStringEnum> ResultModel getEnumVoResult(T[] dataList) {
        List<BasicStringEnumVO> enumList = getEnumVo(dataList);
        return ResultUtil.success(enumList);
    }

    /**
     * 校验编码是否存在
     *
     * @param code
     * @param list
     * @return
     */
    public static <T extends BasicEnum> boolean checkCode(Integer code, T[] list) {
        for (T basicEnum : list) {
            if (Objects.equals(basicEnum.getCode(), code)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 校验名称是否存在
     *
     * @param name
     * @param list
     * @return
     */
    public static <T extends BasicEnum> boolean checkName(String name, T[] list) {
        for (T basicEnum : list) {
            if (StringUtil.equals(basicEnum.getName(), name)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 根据编码获取数据对象
     *
     * @param code
     * @param list
     * @return
     */
    public static <T extends BasicEnum> T getEnumByCode(Integer code, T[] list) {
        for (T basicEnum : list) {
            if (Objects.equals(basicEnum.getCode(), code)) {
                return basicEnum;
            }
        }
        return null;
    }

    /**
     * 获取编码到名称映射
     *
     * @param list
     * @return
     * @param <T>
     */
    public static <T extends BasicEnum> Map<Integer, String> getCodeToNameMap(T[] list) {
        Map<Integer, String> map = new HashMap<>();
        for (T basicEnum : list) {
            map.put(basicEnum.getCode(), basicEnum.getName());
        }
        return map;
    }

}
